package com.xindaoapp.happypet.viewpager.windows;

import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.xindaoapp.happypet.R;
import com.xindaoapp.happypet.XDHttpClient;
import com.xindaoapp.happypet.adapter.MyBaseAdapter;
import com.xindaoapp.happypet.bean.PetInfo;
import com.xindaoapp.happypet.bean.PetInfo.Pet;
import com.xindaoapp.happypet.utils.CommonUtil;
import com.xindaoapp.happypet.utils.Constants;
import com.xindaoapp.happypet.utils.xdConfig;
import com.xindaoapp.happypet.view.CircleImageView;
import com.xindaoapp.happypet.viewpager.ViewPagerWindow;

/**
 * 用户我的爱
 * 
 * @author chenghao
 * 
 */
public class UserMyLoveWindow extends ViewPagerWindow {
    private final String uid;
    private PetListAdapter petAdapter;
    private ListView listView;

    public UserMyLoveWindow(Activity context, String uid) {
        super(context);
        this.uid = uid;
    }

    @Override
    protected View onCreateView() {
        return View.inflate(getBaseContext(), R.layout.fragment_other_tab1, null);
    }

    @Override
    protected void initViews() {
        super.initViews();
        listView = (ListView) getView().findViewById(R.id.listView);
        listView.setOnScrollListener(new OnScrollListener() {
            boolean is = false;

            @Override
            public void onScrollStateChanged(AbsListView arg0, int arg1) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount,
                    int totalItemCount) {
                // TODO Auto-generated method stub
                if (firstVisibleItem >= 3) {
                    is = true;
                } else {
                    is = false;
                }

                if (is) {

                    Intent intent = new Intent(Constants.SCROLL_FOR);
                    intent.putExtra("go", "up");
                    intent.putExtra("from", "0");
                    getBaseContext().sendBroadcast(intent);
                } else {
                    Intent intent = new Intent(Constants.SCROLL_FOR);
                    intent.putExtra("go", "down");
                    intent.putExtra("from", "0");
                    getBaseContext().sendBroadcast(intent);
                }


            }
        });
    }

    @Override
    protected void onLoadDatas() {
        super.onLoadDatas();
        getData();
    }

    private void getData() {
        if (CommonUtil.checkNetState(mContext)) {
            RequestParams params = new RequestParams();
            params.put("uid", uid);
            XDHttpClient.get(xdConfig.MYPETLIST, params, new AsyncHttpResponseHandler() {
                @Override
                public void onSuccess(int statusCode, String content) {
                    try {
                        PetInfo entity = JSON.parseObject(content, PetInfo.class);
                        if (entity.getResult().equals("0")) {
                            if (entity.getArray().size() == 0) {
                                onNoDataArrived();
                            } else {
                                onDataArrived(true);
                                petAdapter =
                                        new PetListAdapter(mContext, entity.getArray(),
                                                R.layout.layout_pet_item);
                                listView.setAdapter(petAdapter);
                            }
                        } else {
                            onDataArrived(false);
                            Toast.makeText(mContext, "数据获取错误", Toast.LENGTH_SHORT).show();
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }

                @Override
                public void onFailure(Throwable arg0, String arg1) {
                    super.onFailure(arg0, arg1);
                    onDataArrived(false);
                    Toast.makeText(mContext, R.string.pLease_check_network, 0).show();
                }
            });
        } else {
            Toast.makeText(mContext, R.string.pLease_check_network, 0).show();
            return;
        }
    }

    class PetListAdapter extends MyBaseAdapter<Pet> {

        public PetListAdapter(Context context, List<Pet> mList, int resId) {
            super(context, mList, resId);
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent, Pet t) {
            ViewHolder holder = (ViewHolder) convertView.getTag();
            if (null == holder) {
                holder = new ViewHolder();
                holder.pet_icon = (CircleImageView) convertView.findViewById(R.id.pet_icon);
                holder.tv_peg_name = (TextView) convertView.findViewById(R.id.tv_peg_name);
                holder.tv_peg_type = (TextView) convertView.findViewById(R.id.tv_peg_type);
                holder.tv_peg_type2 = (TextView) convertView.findViewById(R.id.tv_peg_type2);
                holder.iv_peg_sex = (ImageView) convertView.findViewById(R.id.iv_peg_sex);
                convertView.setTag(holder);
            }
            String str;

            ImageLoader.getInstance().displayImage(t.getCover(), holder.pet_icon);

            holder.tv_peg_name.setText(t.getTitle());

            holder.tv_peg_type2.setText(t.getBreed());

            str = t.getSex();

            if (str.equals("2")) {
                holder.iv_peg_sex.setBackgroundResource(R.drawable.icon_women);
            } else {
                holder.iv_peg_sex.setBackgroundResource(R.drawable.icon_men);
            }

            str = t.getPettype();

            if (str.equals("1")) {
                holder.tv_peg_type.setText("汪星人");
            }
            if (str.equals("2")) {
                holder.tv_peg_type.setText("喵星人");
            }
            if (str.equals("3")) {
                holder.tv_peg_type.setText("小宠星人");
            }
            if (str.equals("4")) {
                holder.tv_peg_type.setText("水星人");
            }

            return convertView;
        }

        class ViewHolder {
            CircleImageView pet_icon;
            ImageView iv_peg_sex;
            TextView tv_peg_name;
            TextView tv_peg_type;
            TextView tv_peg_type2;
        }

    }
}
